using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class Pages_Employee_ManageCustomers_ViewCustomers : System.Web.UI.Page
{
    ArrayList selectedItems = new ArrayList();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            Session["ViewCustomersSelectMethod"] = null;
        if (Session["ViewCustomersSelectMethod"] != null)
            customersDataSource.SelectMethod = Session["ViewCustomersSelectMethod"].ToString();
    }

    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        // if radio button Customer ID is checked, then the text in search box 
        // must be a positive integer
        if (rdCustomerID.Checked)
        {
            try
            {
                int n = Int32.Parse(args.Value);
                args.IsValid = (n > 0);
            }
            catch
            {
                args.IsValid = false;
            }
        }
    }

    protected void gvCustomers_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvCustomers.PageIndex = e.NewPageIndex;
        gvCustomers.DataBind();
    }

    protected void btnAddCustomer_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Pages_Employee/ManageCustomers/AddCustomer.aspx");
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        lblMessage.Text = "";
        customersDataSource.SelectParameters.Clear();
        if (txtSearch.Text != "" && Page.IsValid)
        {
            string criteria = txtSearch.Text.Trim();
            if (rdCustomerID.Checked)
            {
                // save the datasource select method to session, for paging after searching
                Session["ViewCustomersSelectMethod"] = "GetCustomersByID";
                customersDataSource.SelectMethod = "GetCustomersByID";
                customersDataSource.SelectParameters.Add("CustomerID", criteria);
            }
            else if (rdFullName.Checked)
            {
                Session["ViewCustomersSelectMethod"] = "GetCustomersByFullName";
                customersDataSource.SelectMethod = "GetCustomersByFullName";
                customersDataSource.SelectParameters.Add("FullName", criteria);
            }
        }
        else
        {
            Session["ViewCustomersSelectMethod"] = "GetCustomers";
            customersDataSource.SelectMethod = "GetCustomers";
        }
        BindData();
    }

    protected void btnDeleteCustomers_Click(object sender, EventArgs e)
    {
        // get the selected items
        foreach (GridViewRow row in gvCustomers.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                CheckBox cboDelete = (CheckBox)row.Cells[0].FindControl("cboDelete");
                if (cboDelete != null)
                {
                    if (cboDelete.Checked)
                    {
                        string id = row.Cells[0].Text;
                        selectedItems.Add(id);
                    }
                }
            }
        }

        // process the deleting
        if (selectedItems.Count == 0)
        {
            lblMessage.Text = "Please select some item first";
        }
        else
        {
            int affected = 0;
            try
            {
                affected = DALCustomers.DeleteCustomers(selectedItems);
                if (affected < selectedItems.Count)
                    lblMessage.Text = "One or more items cannot be deleted";
                else
                    lblMessage.Text = "Deleted successfully";
                BindData();
            }
            catch (SqlException ex)
            {
                lblMessage.Text = "There was an error, the operation was terminated";
            }
        }
    }

    private void BindData()
    {
        customersDataSource.DataBind();
        gvCustomers.DataBind();
    }
}
